package com.forcht.client.view;

import com.forcht.client.Client;

import javax.swing.*;
import java.awt.event.*;

/**
 * Created by forcht on 2018/4/12.
 * 客户端登陆界面
 */
public class LoginView extends JFrame implements ActionListener,WindowListener{

    //定义中部需要的组件
    private JPanel centerPanel;
    private JLabel usernameLabel, passwordLabel;
    private JTextField usernameTextField;
    private JPasswordField passwordField;
    private Client client;
    //定义南部需要的组件
    private JPanel southPanel;
    private JButton loginButton, cancelButton, registerButton;

    public static void main(String[] args) {
        new LoginView();
    }


    /**
     * 初始化中间组件
     */
    public void initCenterLayout(){
        centerPanel =new JPanel();
        centerPanel.setLayout(null);
        usernameLabel =new JLabel("用户名:");
        usernameLabel.setBounds(30, 15, 60, 25);
        passwordLabel =new JLabel("用户密码:");
        passwordLabel.setBounds(30, 50, 60, 25);
        usernameTextField =new JTextField();
        usernameTextField.setBounds(80, 15, 150, 25);
        passwordField =new JPasswordField();
        passwordField.setBounds(80, 50, 150, 25);
        centerPanel.add(usernameLabel);
        centerPanel.add(usernameTextField);
        centerPanel.add(passwordLabel);
        centerPanel.add(passwordField);
    }

    /**
     * 初始化南部的组件
     */
    public void initSouthLayout(){
        southPanel =new JPanel();
        loginButton =new JButton("登录");
        loginButton.addActionListener(this);
        cancelButton =new JButton("取消");
        cancelButton.addActionListener(this);
        registerButton =new JButton("注册");
        registerButton.addActionListener(this);
        southPanel.add(loginButton);
        southPanel.add(cancelButton);
        southPanel.add(registerButton);
    }

    public LoginView(){
        //布局中部
        this.initCenterLayout();
        //布局南部
        this.initSouthLayout();
        //监听窗口事件
        this.addWindowListener(this);

        this.add(centerPanel,"Center");
        this.add(southPanel,"South");

        this.setTitle("登录界面");
        this.setResizable(false);
        this.setSize(300, 200);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==loginButton){
            //如果已经登录了
            if(client!=null&&client.getUser()!=null){
                return;
            }
            String username = usernameTextField.getText();
            char[] password = passwordField.getPassword();
            client=new Client();
            client.setLoginView(this);
            client.launch();
            client.login(username,password);
        }
    }


    @Override
    public void windowOpened(WindowEvent e) {

    }

    @Override
    public void windowClosing(WindowEvent e) {
        //关闭连接
        if(client!=null){
            client.close();
        }
    }

    @Override
    public void windowClosed(WindowEvent e) {
    }

    @Override
    public void windowIconified(WindowEvent e) {

    }

    @Override
    public void windowDeiconified(WindowEvent e) {

    }

    @Override
    public void windowActivated(WindowEvent e) {

    }

    @Override
    public void windowDeactivated(WindowEvent e) {

    }
}
